package com.owulia.leetcode.string;

public class LongestCommonPrefix {

    public static String longestCommonPrefix(String[] strs) {
        if (null == strs || strs.length == 0) {
            return "";
        }
        String fristStr = strs[0];
        int min = fristStr.length();
        for (int i = 1; i < strs.length && min > 0; i++) {
            String str = strs[i];
            int maxtemp = 0;
            for (int j = 0; j < fristStr.length() && j < str.length() && maxtemp <= min && fristStr.charAt(j) == str.charAt(j); j++) {
                maxtemp++;
            }
            if (maxtemp < min) {
                min = maxtemp;
            }
        }
        return fristStr.substring(0, min);
    }

    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"dog", "car", "car"}));
    }


    public int lengthOfLastWord(String s) {
        int count = 0;
        boolean hasBegin = false;
        for (int i = s.length() - 1; i >= 0; i--) {
            if (Character.isSpaceChar(s.charAt(i))) {
                if (hasBegin) {
                    break;
                }
            } else {
                count++;
                hasBegin = true;
            }
        }
        return count;
    }
}
